# -*- encoding: utf-8 -*-
# @Author：lijinxi
# @Time ：2021/1/13 11:14
# @File：average.py


import numpy as np
import pandas as pd
from scipy import stats, integrate
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

msgs = []
time_use = []
score = []
memory = []

with open('summary.csv') as f:
    for line in f:
        if line.startswith('M'):
            msg = line.strip().split(',')
            msgs.append(msg)
            time_use.append(msg[3] if float(msg[3]) < 10 else '10')
            score.append(msg[2])
            memory.append(msg[4])

# 绘制图形,时间评率直方图
plt.figure(dpi=600)
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
# seaborn中文乱码解决方案
font = FontProperties(fname=r'C:\Windows\Fonts\simhei.ttf', size=14)
sns.set(font=font.get_name())
sns.distplot(time_use, hist=False, rug=True)
plt.xticks([0.5, 2, 4, 6, 8, 10, 12])
plt.xlim(0, 10)
plt.xlabel('Time Use')
# plt.savefig('time_use.png')
# plt.show()

plt.figure(dpi=600)
sns.distplot(score, hist=False, rug=True)
plt.xlabel('score')
# plt.savefig('score.png')
# plt.show()

sorted_msgs = sorted(msgs, key=lambda _msg: float(_msg[3]), reverse=True)
print('max:')
for msg in sorted_msgs[:10]:
    print(str(msg).replace('[', '').replace(']', '').replace('\'', ''))
print('min:')
for msg in sorted_msgs[-1:-11:-1]:
    print(str(msg).replace('[', '').replace(']', '').replace('\'', ''))

print(time_use, memory, score)
time_use_num = [float(time) for time in time_use]
memory_num = [int(mem) for mem in memory]
score_num = [int(_score) for _score in score]
print(sum(time_use_num) / len(time_use), sum(memory_num) / len(memory), sum(score_num) / len(score))
